package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.aibkAnalysisZHT

import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.AnalysisInforIndexHelp.getChildListBean
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_item_child_xzt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_childask.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*

/**
 * auther: 王国龙
 * Date：2020.05.22
 * describe:
 */
class AnalysisAdapterChildXzt(
        val activity:AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor,
        val index_A:Int,
        val index_B:Int,
        val index_C:Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        var RootView: View? = null
        if (position == getChoiceTotalSize() + 1) {
            RootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_itemanalysis, parent, false)
        }else if (position == 0) {
            RootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_childask, parent, false)
        } else {
            RootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_child_xzt, parent, false)
        }
        return RootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        if (position == getChoiceTotalSize() + 1) {
            val childAnalysis = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAnalysis
            CustomTextViewToPicture(activity, holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(childAnalysis))
        }else if (position == 0) {
            val sortIndex = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[0].SortIndex
            val childAsk = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAsk
            holder.convertView.TvChildAskPositionX.text = "$sortIndex. "
            CustomTextViewToPicture(activity, holder.convertView.TvChildAsk, childAsk)

            //只有当小题中含有音频题干时才显示小题音频播放器，其余时候隐藏播放器
            val path = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAudio.Path
            if (path.isNotEmpty()) {
                holder.convertView.IvChildAudioPlayA.visibility = View.VISIBLE
                childAudioListener!!.onInitChildAudioPlayerA(path, holder.convertView.IvChildAudioPlayA)
            } else {
                holder.convertView.IvChildAudioPlayA.visibility = View.GONE
            }

        } else {
            val optionIndex = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildOptionList[position-1].OptionIndex
            val option = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildOptionList[position-1].Option
            val showType = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildOptionList[position-1].ShowType
            if(showType == 0) {
                holder.convertView.BtnOptionIndex.text = optionIndex
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, option)
            }else{
                holder.convertView.BtnOptionIndex.text = option
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, "")
            }

            var stuAnswer = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[index_C].ItemList[0].StuAnswer
            val itemAnswer = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[0].ItemAnswer
            if (optionIndex == itemAnswer) {
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
                if (itemAnswer == stuAnswer) {
                    holder.convertView.BtnDuiGou.setVisibility(View.VISIBLE)
                    holder.convertView.BtnOptionIndex.setTextColor(activity.getResources().getColor(R.color.transparent))
                } else {
                    holder.convertView.BtnDuiGou.setVisibility(View.GONE)
                    holder.convertView.BtnOptionIndex.setTextColor(activity.getResources().getColor(R.color.lancoooWhite))
                }
            } else {
                holder.convertView.BtnDuiGou.setVisibility(View.GONE)
                if (optionIndex == stuAnswer) {
                    holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_red)
                    holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.lancoooWhite))
                } else {
                    holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_white)
                    holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.textColor))
                }
            }
        }

    }

    private fun getChoiceTotalSize(): Int {
        return getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildOptionList.size
    }

    override fun getItemCount(): Int {
        return getChoiceTotalSize() + 2
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    interface OnChildAudioListener {
        fun onInitChildAudioPlayerA(path: String, ivPlay: ImageView)
    }

    private var childAudioListener: OnChildAudioListener? = null

    fun setChildAudioListener(childAudioListener: OnChildAudioListener?) {
        this.childAudioListener = childAudioListener
    }

}